// 2025/3/21
// https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/description/?envType=study-plan-v2&envId=top-100-liked
// 在排序数组中查找元素的第一个位置和最后一个位置

class Solution {
    public:
        vector<int> searchRange(vector<int>& nums, int target) {
            int left = 0, right = nums.size() - 1;
            int mid = (left + right) / 2;
            while(left <= right)
            {
                mid = (left + right) / 2;
                if(nums[mid] > target)
                    right = mid - 1;
                else if(nums[mid] < target)
                    left = mid + 1;
                else
                {
                    left = right = mid;
                    while(left > 0 && nums[left - 1] == target) left--;
                    while(right < nums.size() - 1 && nums[right + 1] == target) right++;
                    return (vector<int>){left, right};
                }
            }
            return (vector<int>){-1, -1};
        }
    };